#include <iostream>
using std::cout;
using std::endl;

class Data{

public:
    Data(){
        cout << "Data()" << endl;
    }

    ~Data(){
        cout << "~Data()" << endl;
    }
    int getData(){
        return _data;
    }

private:
    int _data = 10;
};

class MiddleLayer{
public:
    MiddleLayer(Data * p)
    :_pdata(p)
    {
        cout << "MiddleLayer(Data *)" << endl; 
    }

    ~MiddleLayer(){
        cout << "~MiddleLayer()" << endl;
        if(_pdata){
            delete _pdata;
            _pdata = nullptr;
        }
    }
    Data * operator->(){
        return _pdata;
    }
    //对象能够进行解引用，只有一个原因
    //实现了解引用运算符的重载
    Data & operator*(){
        return *_pdata;
    }

private:
    Data * _pdata;
};

class ThirdLayer{
public:
    ThirdLayer(MiddleLayer * p)
    :_ml(p)
    {
        cout << "ThirdLayer(MiddleLayer*)" << endl;
    }
    ~ThirdLayer(){
        cout << "~ThirdLayer()" << endl;
        if(_ml){
            delete _ml;
            _ml = nullptr;
        }
    }
    //箭头访问运算符
    MiddleLayer & operator->(){
        return *_ml;
    }
    Data & operator*(){
        return *(*_ml);
    }

private:
    MiddleLayer * _ml;
};


void test4(){
    ThirdLayer tl(new MiddleLayer(new Data()));
    //目前的代码满足不了这个功能
    cout << (*tl).getData() << endl;
}
int main()
{
    return 0;
}

